/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.employee.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.employee.entity.AttendanceRecords;
import com.thinkgem.jeesite.modules.employee.entity.Employee;
import com.thinkgem.jeesite.modules.employee.entity.EmployeeLeave;
import com.thinkgem.jeesite.modules.employee.service.AttendanceRecordsService;

/**
 * 考勤结果统计Controller
 * @author pan.zhou
 * @version 2017-07-21
 */
@Controller
@RequestMapping(value = "${adminPath}/employee/attendanceRecords")
public class AttendanceRecordsController extends BaseController {

	@Autowired
	private AttendanceRecordsService attendanceRecordsService;
	
	@ModelAttribute
	public AttendanceRecords get(@RequestParam(required=false) String id) {
		AttendanceRecords entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = attendanceRecordsService.get(id);
		}
		if (entity == null){
			entity = new AttendanceRecords();
		}
		return entity;
	}
	
	@RequiresPermissions("employee:attendanceRecords:view")
	@RequestMapping(value = {"list", ""})
	public String list(AttendanceRecords attendanceRecords, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AttendanceRecords> page = attendanceRecordsService.findPage(new Page<AttendanceRecords>(request, response), attendanceRecords); 
		
		model.addAttribute("page", page);
		return "modules/employee/attendanceRecordsList";
	}

	@RequiresPermissions("employee:attendanceRecords:view")
	@RequestMapping(value = "form")
	public String form(AttendanceRecords attendanceRecords, Model model) {
		model.addAttribute("attendanceRecords", attendanceRecords);
		return "modules/employee/attendanceRecordsForm";
	}

	@RequiresPermissions("employee:attendanceRecords:edit")
	@RequestMapping(value = "save")
	public String save(AttendanceRecords attendanceRecords, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, attendanceRecords)){
			return form(attendanceRecords, model);
		}
		attendanceRecordsService.save(attendanceRecords);
		addMessage(redirectAttributes, "保存考勤结果成功");
		return "redirect:"+Global.getAdminPath()+"/employee/attendanceRecords/?repage";
	}
	
	@RequiresPermissions("employee:attendanceRecords:edit")
	@RequestMapping(value = "delete")
	public String delete(AttendanceRecords attendanceRecords, RedirectAttributes redirectAttributes) {
		attendanceRecordsService.delete(attendanceRecords);
		addMessage(redirectAttributes, "删除考勤结果成功");
		return "redirect:"+Global.getAdminPath()+"/employee/attendanceRecords/?repage";
	}
	
	/**
     * 
     * 查询异常打卡列表
     * @param employee
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("employee:attendanceRecords:view")
    @RequestMapping(value = {"queryExceptionEmployee", ""})
    public String queryExceptionEmployee(AttendanceRecords attendanceRecords, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AttendanceRecords> page = attendanceRecordsService.queryExceptionEmployee(new Page<AttendanceRecords>(request, response), attendanceRecords); 
        model.addAttribute("page", page);
        return "modules/employee/exceptionEmployee";
    }




    /**
     * 员工考勤统计
     * @param EmployeeLeave
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("employee:attendanceRecords:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(AttendanceRecords attendanceRecords, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "员工考勤统计信息数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<AttendanceRecords> page = attendanceRecordsService.findPage(new Page<AttendanceRecords>(request, response, -1), attendanceRecords);
            new ExportExcel("员工考勤统计信息数据", AttendanceRecords.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出员工考勤统计失败！失败信息："+e.getMessage());
        }
        return "redirect:" + adminPath + "/employee/attendanceRecords/list?repage";
    }



}